/**
 * <p>
 * 版权所有：中企动力
 * </p>
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 * </p>
 */
package com.ce.pms.common.dao.impl;

import java.math.BigInteger;
import java.util.List;

import org.hibernate.Query;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.common.dao.ISecRightDao;
import com.ce.pms.common.model.SecRight;

/**
 * 
 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
 * @Created 2008-10-22
 */

public class SecRightDaoImpl extends PmsBaseDaoImpl<SecRight> implements
		ISecRightDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ce.pms.common.dao.ISecRightDao#getBuildingRight(java.lang.Long)
	 */
	public Long[] getBuildingRight(Long userId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("SELECT SRB.BUILDING_ID FROM SEC_RIGHT_BUILDING AS SRB");
		buffer
				.append(" INNER JOIN SEC_RIGHT AS SR ON SRB.BUILDING_RIGHT_ID = SR.RIGHT_ID");
		buffer
				.append(" WHERE SR.RIGHT_ID IN  (SELECT SURR.RIGHT_ID FROM SEC_USER_R_RIGTHS SURR WHERE SURR.USER_ID =");
		buffer.append(userId);
		buffer.append(" )");
		Query query = super.createSQLQuery(buffer.toString(), null);
		List resultList = query.list();
		Long[] ids = new Long[resultList.size()];
		for (int i = 0; i < ids.length; i++) {
			ids[i] = ((BigInteger) resultList.get(i)).longValue();
		}
		return ids;
	}

	public Long[] getPrecinctRight(Long userId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("SELECT SRP.PRECINCT_ID FROM SEC_RIGHT_PRECINCT AS SRP");
		buffer
				.append(" INNER JOIN SEC_RIGHT AS SR ON SRP.RIGHT_PRECINCT_ID = SR.RIGHT_ID");
		buffer
				.append(" WHERE SR.RIGHT_ID IN  (SELECT SURR.RIGHT_ID FROM SEC_USER_R_RIGTHS SURR WHERE SURR.USER_ID =");
		buffer.append(userId);
		buffer.append(" )");
		Query query = super.createSQLQuery(buffer.toString(), null);
		List resultList = query.list();
		Long[] ids = new Long[resultList.size()];
		for (int i = 0; i < ids.length; i++) {
			ids[i] = ((BigInteger) resultList.get(i)).longValue();
		}
		return ids;
	}
}
